package com.flyco.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.modules.oms.entity.OmsOrderTrace;
import com.flyco.modules.oms.mapper.OmsOrderTraceMapper;
import com.flyco.modules.oms.service.IOmsOrderTraceService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description: 订单跟踪
 * @Author: flyco
 * @Date: 2020-03-27
 * @Version: V1.0
 */
@Service
public class OmsOrderTraceServiceImpl extends ServiceImpl<OmsOrderTraceMapper, OmsOrderTrace> implements IOmsOrderTraceService {

    @Autowired
    private OmsOrderTraceMapper traceMapper;

    @Override
    public OmsOrderTrace getBySendOrderId(String sendOrderId) {
        LambdaQueryWrapper<OmsOrderTrace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrderTrace::getSendOrderId, sendOrderId);
        List<OmsOrderTrace> traceList = traceMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(traceList)) {
            return null;
        } else {
            return traceList.get(0);
        }
    }

    @Override
    public List<OmsOrderTrace> getOrderTraceList(String orderId) {
        LambdaQueryWrapper<OmsOrderTrace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrderTrace::getOrderId, orderId);
        List<OmsOrderTrace> traceList = traceMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(traceList)) {
            return null;
        } else {
            return traceList;
        }
    }

    @Override
    public void saveTrace(String orderId, String label, String content) {

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        OmsOrderTrace omsOrderTrace = new OmsOrderTrace();
        omsOrderTrace.setOrderId(orderId);
        omsOrderTrace.setLabel("作废");
        if (!StringUtils.isEmpty(content)) {
            omsOrderTrace.setContent(content);
        }
        omsOrderTrace.setCreateTime(date);
        omsOrderTrace.setCreateBy(sysUser.getRealname());
        save(omsOrderTrace);
    }
}
